﻿using engine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public class CustomForm: Form
    {
        public CustomForm()
        {
        }

        public void ShowFileDialog(string title, string filter, bool multiselect, Action<OpenFileDialog> action)
        {

            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = multiselect;
            fileDialog.Title = title;
            fileDialog.Filter = filter;// "所有文件(*doc*)|*.doc*"; //设置要选择的文件的类型
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    action(fileDialog);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
        }

        public void ShowFolderDialog(string title, Action<FolderBrowserDialog> action)
        {

            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = title;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }
                action(dialog);
            }
        }
    }


    public class RenderTask
    {
        public string TaskName { get; set; }
        public RenderScheme RenderScheme { get; set; }
        public List<RenderTaskItem> Items { get; set; }
        public string OutputPath { get; set; }
        public RenderTaskStatus Status { get; set; }

        public EngineRuntime EngineRuntime { get; private set; }

        public RenderTask()
        {
            EngineRuntime = new EngineRuntime();
            Items = new List<RenderTaskItem>();
        }

        public void Reset()
        {
            Status = RenderTaskStatus.Ready;
            foreach (var item in Items)
            {
                item.Status = RenderTaskItemStatus.Ready;
            }
            //if (Status == RenderTaskStatus.OK || Status == RenderTaskStatus.Error)
            //{
            //    DialogResult result = MessageBox.Show("重新执行该任务吗？", "执行", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //    if (result == DialogResult.Yes)
            //    {
            //        foreach (var item in Items)
            //        {
            //            item.Status = RenderTaskItemStatus.Ready;
            //        }
            //    }
            //    else { return; }
            //}
        }

        public void Run()
        {
            if (RenderScheme == null)
            {
                MessageBox.Show("未选择任务方案!");
                return;
            }

            EngineRuntime.Hook.BeforeTask();
            Status = RenderTaskStatus.Running;

            while (true)
            {
                bool ok = true;

                foreach (RenderTaskItem item in Items)
                {
                    if (item.Status == RenderTaskItemStatus.Ready || item.Status == RenderTaskItemStatus.Running)
                    {
                        ok = false;
                        EngineRuntime.Hook.BeforeTrans();
                        RunItem(item);
                        EngineRuntime.Hook.AfterTrans();
                    }
                }

                if (ok) break;
            }

            Status = RenderTaskStatus.OK;
            EngineRuntime.Hook.AfterTask();
        }

        public void Stop()
        {

        }

        public void RunItem(RenderTaskItem item)
        {
            item.Status = RenderTaskItemStatus.Running;

            FileInfo fileInfo = new FileInfo(RenderScheme.TargetTempalteName);
            item.TargetName = OutputPath + "/" + DateTime.Now.Ticks.ToString() + fileInfo.Extension;

            // run
            Engine eg = new Engine(EngineRuntime);
            eg.SetRenderTemplateFunc(new engine.TijianTemplateFunc());
            Dictionary<string, DataItem> data = eg.MatchFile(
                RenderScheme.SourceTempalteName,
                RenderScheme.SourceConfigName,
                item.SourceName);
            eg.RenderTemplate(
                RenderScheme.TargetTempalteName,
                RenderScheme.TargetConfigName,
                data,
                item.TargetName);

            item.Status = RenderTaskItemStatus.OK;
        }
    }

    public class RenderScheme
    {
        public string SchemeName { get; set; }
        public string SourceTempalteName { get; set; }
        public string SourceConfigName { get; set; }
        public string TargetTempalteName { get; set; }
        public string TargetConfigName { get; set; }

        public RenderScheme Clone()
        {
            RenderScheme scheme = new RenderScheme
            {
                SchemeName = SchemeName,
                SourceTempalteName = SourceTempalteName,
                SourceConfigName = SourceConfigName,
                TargetTempalteName = TargetTempalteName,
                TargetConfigName = TargetConfigName
            };
            return scheme;
        }
    }

    public class RenderTaskItem
    {
        public string SourceName { get; set; }
        public string TargetName { get; set; }
        public RenderTaskItemStatus Status { get; set; }
    }

    public enum RenderTaskStatus
    {
        Ready,
        Running,
        Error,
        OK
    }

    public enum RenderTaskItemStatus
    {
        Ready,
        Running,
        Error,
        OK
    }
}
